#ifndef __HELPLAYER_H__
#define __HELPLAYER_H__
#include "cocos2d.h"
USING_NS_CC;

namespace suta
{
	class HelpLayer:public CCLayer
	{
		CCSprite*m_pSprite;
		int m_iIndex;

		const float m_cfBigScale;
		const float m_cfSmallScale;

		CCSprite*m_pRightArrow;
		CCSprite*m_pLeftArrow;
		CCSprite*m_pBackToMenu;

		CCSprite*m_aPoints[8];

		void set(bool right=true)
		{
			static const char *path[8]={"help/help_0.png","help/help_1.png","help/help_2.png","help/help_3.png","help/help_4.png","help/help_5.png","help/help_6.png","help/help_7.png"};
			
			m_iIndex+=(right?1:-1);
			
			if (m_iIndex<0)
			{
				m_iIndex=0;
			}else if(m_iIndex>7)
			{
				MenuScene*scene=MenuScene::node();
				CCDirector::sharedDirector()->replaceScene(scene);
				return;
			}


			m_pLeftArrow->setIsVisible(m_iIndex!=0);
			m_pRightArrow->setIsVisible(m_iIndex!=7);
			m_pBackToMenu->setIsVisible(!m_pRightArrow->getIsVisible());
	
			m_pSprite->setTexture(CCTextureCache::sharedTextureCache()->addImage(path[m_iIndex]));

			for (int i=0;i<8;i++)
			{
				m_aPoints[i]->setScale(i==m_iIndex?m_cfBigScale:m_cfSmallScale);
			}
		};
	public:
		HelpLayer():
		  CCLayer(),
		  m_cfBigScale(0.5f),
		  m_cfSmallScale(0.3f),
		  m_iIndex(0){};
		bool init()
		{
			bool ret=false;
			do 
			{
				CC_BREAK_IF(!CCLayer::init());
				setIsTouchEnabled(true);
				m_iIndex=0;
				{
					//bg
					CCSprite*bg=CCSprite::spriteWithFile("help/help.png");
					bg->setPosition(ccp(400,240));
					addChild(bg);

					CCSprite*halfOpcity=CCSprite::spriteWithFile("bg/bg_dialog.png");
					halfOpcity->setPosition(ccp(400,240));
					addChild(halfOpcity);
				}
				m_pSprite=CCSprite::spriteWithFile("help/help_0.png");
				m_pSprite->setPosition(ccp(400,240));
				addChild(m_pSprite);

				{
					//arrow
					m_pRightArrow=CCSprite::spriteWithFile("arrow.png");
					m_pRightArrow->setPosition(ccp(740,240));
					addChild(m_pRightArrow);

					m_pLeftArrow=CCSprite::spriteWithFile("arrow.png");
					m_pLeftArrow->setPosition(ccp(60,240));
					m_pLeftArrow->setRotation(180.0f);
					addChild(m_pLeftArrow);
					m_pLeftArrow->setIsVisible(false);
				}

				{
					m_pBackToMenu=CCSprite::spriteWithFile("back_to_menu.png");
					m_pBackToMenu->setPosition(m_pRightArrow->getPosition());
					m_pBackToMenu->setIsVisible(false);
					addChild(m_pBackToMenu);


					CCScaleTo*scaleOut=CCScaleTo::actionWithDuration(0.8f,1.2f);
					CCScaleTo*scaleIn=CCScaleTo::actionWithDuration(0.8f,1.0f);
					CCSequence*seq=CCSequence::actionOneTwo(scaleOut,scaleIn);
					CCRepeatForever*repeat=CCRepeatForever::actionWithAction(seq);
					m_pBackToMenu->runAction(repeat);
				}

				{
					for (int i=0;i<8;i++)
					{
						m_aPoints[i]=CCSprite::spriteWithTexture(CCTextureCache::sharedTextureCache()->addImage("ball.png"));
						m_aPoints[i]->setPosition(ccp(400-105+i*30,420));
						m_aPoints[i]->setScale(m_cfSmallScale);
						addChild(m_aPoints[i]);

						CCRotateTo*rotate=CCRotateTo::actionWithDuration(1.0f,360.0f);
						CCRepeatForever*repeat=CCRepeatForever::actionWithAction(rotate);
						m_aPoints[i]->runAction(repeat);
					}
					m_aPoints[0]->setScale(m_cfBigScale);
				}
				ret=true;
			} while (0);
			return ret;
		};
		LAYER_NODE_FUNC(HelpLayer);
		bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent){return true;};
		void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)
		{
			CCPoint p=CCDirector::sharedDirector()->convertToGL(pTouch->locationInView());
			
			if (p.x>400)
			{
				set();
			}
			else
			{
				set(false);
			}
		};
		
		void onExit()
		{
			CCTextureCache::sharedTextureCache()->removeAllTextures();
			CCLayer::onExit();
		};
		void registerWithTouchDispatcher()
		{
			CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this,0,true);
		};
	};
}//namespace suta
#endif//__HELPLAYER_H__